package com.fs.controller;

import com.fs.service.LoginService;
import com.fs.service.impl.TokenService;
import com.fs.util.ResponseData;
import com.fs.util.ResponseDataUtil;
import com.fs.util.ResultEnums;
import com.fs.util.ServletUtils;
import com.fs.vo.LoginAdmins;
import com.fs.vo.LoginBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * 登录验证
 *
 */
@RestController
public class LoginController {
    @Autowired
    private LoginService loginService;

    @Autowired
    private TokenService tokenService;
    /**
     * 登录方法
     * 
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping("/login")
    public ResponseData login(@RequestBody LoginBody loginBody) throws Exception
    {
        // 生成令牌
        ResponseData login = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
                loginBody.getUuid());
        System.out.println(login);
        return login;
    }

    @RequestMapping("/logout")
    public ResponseData logout()  throws Exception{
        LoginAdmins loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        if (Objects.nonNull(loginUser))
        {
            // 删除用户缓存记录
            tokenService.delLoginUser(loginUser.getToken());
        }
        return ResponseDataUtil.buildOk("退出成功");
    }
}